package com.smzd.ccms.core;

import java.util.Map;

import org.springframework.validation.BindingResult;

import com.smzd.ccms.domain.Channel;
import com.smzd.ccms.domain.Site;
import com.smzd.core.domain.IOwnedEntity;
import com.smzd.framework.core.actions.AbstractMeetingOwnedController;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.utils.db.ORPage;
import com.smzd.framework.utils.db.RPage;
import com.smzd.utils.SessionHolder;

public abstract class AbstractSiteResourceController<E extends IOwnedEntity<Meeting>> extends
		AbstractMeetingOwnedController<E> {

	public String getListData(Map<String, Object> model, String siteStr, ORPage<Meeting, E> page, BindingResult result)
			throws Exception {
		setCurrentSite(model, siteStr);
		return super.getListData(model, page, result);
	}

	public String toAddPage(Map<String, Object> model, String siteStr, E elem, BindingResult result) throws Exception {
		setCurrentSite(model, siteStr);
		return super.toAddPage(model, elem, result);
	}

	public String postAddData(Map<String, Object> model, String siteStr, E elem, BindingResult result) throws Exception {
		setCurrentSite(model, siteStr);
		return super.postAddData(model, elem, result);
	}

	public String toListPage(Map<String, Object> model, String siteStr, E elem, BindingResult result) throws Exception {
		setCurrentSite(model, siteStr);
		return super.toListPage(model, elem, result);
	}

	public String toEditPage(Map<String, Object> model, String siteStr, E elem, BindingResult result) throws Exception {
		setCurrentSite(model, siteStr);
		return super.toEditPage(model, elem, result);
	}

	public String postEditData(Map<String, Object> model, String siteStr, E elem, BindingResult result)
			throws Exception {
		setCurrentSite(model, siteStr);
		return super.postEditData(model, elem, result);
	}

	public String postRemoveData(Map<String, Object> model, String siteStr, E elem, BindingResult result)
			throws Exception {
		setCurrentSite(model, siteStr);
		return super.postRemoveData(model, elem, result);
	}

	public String toStaticListPage(Map<String, Object> model, String siteStr, RPage<E> page, BindingResult result)
			throws Exception {
		setCurrentSite(model, siteStr);
		return super.toStaticListPage(model, page, result);
	}

	protected Site setCurrentSite(Map<String, Object> model, String siteStr) throws Exception {
		Site site = CompletPluginManager.getSite(siteStr);
		if (site == null) {
			throw new Exception("站点不存在！");
		}
		SessionHolder.setObject(Site.SESSION_KEY_CURRENT_SITE, site);
		model.put("_site_", siteStr);
		model.put("_site", site);
		return site;
	}

	@Override
	protected Meeting getOwner() {
		Site site = SessionHolder.getObject(Site.SESSION_KEY_CURRENT_SITE);
		return site.getOwner();
	}

	protected Site translateSite(String siteStr) {
		return CompletPluginManager.getSite(siteStr);
	}

	protected Channel translateChannel(String siteStr, String channelStr) {
		Site site = translateSite(siteStr);
		return translateChannel(site, channelStr);
	}

	private Channel translateChannel(Site site, String channelStr) {
		return site.getChannel(channelStr);
	}

	// protected Site checkCurrentSite() throws Exception {
	// Site site = getCurrentSite();
	// if (site == null) {
	// throw new Exception("请重新进入管理页面后选择栏目管理");
	// }
	// return site;
	// }

}
